home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10363 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: news.panther.net!nemesis!hammy!not-for-mail
  2. From: gordon@sneaky.lerctr.org (Gordon Burditt)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strncpy bug?
  5. Date: 16 Mar 1996 16:37:17 -0600
  6. Organization: What organization?
  7. Message-ID: <4iffqt$gu4@hammy.lonestar.org>
  8. References: <ccurtis.826776589@ee.fit.edu>
  9. NNTP-Posting-Host: news.hammy.lonestar.org
  10.  
  11. >I was wondering if this is a Linux thing (kernel 1.2.1) or a
  12. >more general 'problem'.  If you call strncpy as such:
  13. >    strncpy( dest, NULL, x );
  14. >it will seg fault.  Is this common among all platforms, or
  15. >just with Linux?
  16. >
  17. >The man page says nothing about it, but I would have presumed
  18. >that if str[n]cpy were passed NULL as a source, it would
  19. >merely catenate the destination.  Did I presume wrong?
  20.  
  21. There are a few ANSI C library functions that permit passing NULL
  22. where a pointer is expected.  Very few.  If it's not documented
  23. as OK, you invoke the wrath of undefined behavior.  A NULL pointer
  24. does not meet the definition of a "string".  I think this is a complete
  25. list of ANSI C functions that accept NULL pointers, but I might have 
  26. missed a couple:
  27.  
  28. (FILE *) NULL
  29.     fflush(NULL) flushes all streams.
  30. (time_t *) NULL
  31.     time(NULL) doesn't store a copy through its first argument
  32.     in addition to the return value.
  33. (void *) NULL
  34.     printf family, NULL argument matching a %p conversion may be printed.
  35.     free(NULL) does nothing.
  36.     realloc(NULL, size) acts like malloc(size).
  37. (char **) NULL
  38.     The 2nd argument of strtod, strtol, and strtoul may be NULL to
  39.     omit returning the pointer to the next character after the converted
  40.     number.
  41. (char *) NULL (strings)
  42.     strtok (1st argument) NULL indicates continuing to use a
  43.     previously passed string.
  44.     strxfrm may have a NULL 1st argument if the length is 0.
  45.     system(NULL) is used to test whether the facility is available.
  46. (char *) NULL (buffer)
  47.     setbuf and setvbuf(2nd argument) NULL indicates unbuffered operation.
  48. (char *) NULL multibyte character functions
  49.     mblen (1st arg), mbtowc (2nd arg), and wctomb(1st arg) accept
  50.     NULL for certain special cases.
  51.  
  52.                     Gordon L. Burditt
  53.                     sneaky.lerctr.org!gordon
  54.  
  55.  
  56.